package org.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.demo.entity.TrainingLog;
import org.example.demo.mapper.TrainingLogMapper;
import org.example.demo.service.TrainingLogService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * 培训日志服务实现类
 */
@Service
public class TrainingLogServiceImpl extends ServiceImpl<TrainingLogMapper, TrainingLog> implements TrainingLogService {

    @Override
    public Page<TrainingLog> selectPage(TrainingLog trainingLog, Integer pageNum, Integer pageSize) {
        Page<TrainingLog> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        QueryWrapper<TrainingLog> queryWrapper = new QueryWrapper<>();

        // 构建查询条件
        queryWrapper.lambda()
                .eq(trainingLog.getUserId() != null, TrainingLog::getUserId, trainingLog.getUserId())
                .eq(trainingLog.getStatus() != null, TrainingLog::getStatus, trainingLog.getStatus())
                .like(trainingLog.getTrainingType() != null && !trainingLog.getTrainingType().isEmpty(),
                        TrainingLog::getTrainingType, trainingLog.getTrainingType())
                .like(trainingLog.getLearningPlace() != null && !trainingLog.getLearningPlace().isEmpty(),
                        TrainingLog::getLearningPlace, trainingLog.getLearningPlace())
                .like(trainingLog.getTrainingTeacher() != null && !trainingLog.getTrainingTeacher().isEmpty(),
                        TrainingLog::getTrainingTeacher, trainingLog.getTrainingTeacher())
                .ge(trainingLog.getRecordTime() != null, TrainingLog::getRecordTime, trainingLog.getRecordTime())
                .orderByDesc(TrainingLog::getRecordTime);

        return this.page(page, queryWrapper);
    }
}